package com.libweb.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.libweb.model.dao.BookDao;
import com.libweb.model.dao.MemberDao;
import com.libweb.model.dto.Book;
import com.libweb.model.dto.Member;

@WebServlet("/book/view.action")
public class BookViewServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		
		//1. 요청 정보에서 조회할 도서번호를 읽고 변수에 저장

		String no = req.getParameter("bookno");
		if (no == null || no.length() == 0) {
			resp.sendRedirect("/libweb/book/list.action");
			return;
		}
		
		int bookno = Integer.parseInt(no);		
		
		BookDao dao = new BookDao();
		Book book = dao.getBookByBookNo(bookno);
		if (book == null) {
			resp.sendRedirect("/libweb/book/list.action");
			return;
		}		
		
		req.setAttribute("book", book);
		
		//3-2. 응답 컨텐츠 생성 (JSP 이용해서 처리 - jsp page로 forward)		
			RequestDispatcher dispatcher = 
					req.getRequestDispatcher("/WEB-INF/views/book/view.jsp");

				dispatcher.forward(req, resp);
		
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
		throws ServletException ,IOException {
		
		doGet(req, resp);
	};

}








